package juc.c_020;
/*
   Talk is cheap. Show me the code.
 */

import java.util.concurrent.Exchanger;

/**
 * @Description
 * @Date 2021/6/20
 * @Author liudl
 */
public class T12_Exchanger {
    static Exchanger<String> exchanger = new Exchanger<>();

    public static void main(String[] args) {
        new Thread(() -> {
            String s = "T1";
            try {
                s = exchanger.exchange(s);
                System.out.println(Thread.currentThread().getName() + " " + s);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "Thread1").start();
        new Thread(() -> {
            String s = "T2";
            try {
                s = exchanger.exchange(s);
                System.out.println(Thread.currentThread().getName() + " " + s);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "Thread2").start();
    }
}
